<?php

namespace Admin\Controller;
use Common\Controller\AuthController;
class SysController extends AuthController {
    public function admin_list(){
        $data = M('admin')->select();
        $this->assign('data',$data);
        $this->display();
    }

    public function admin_state(){
            $id=I('id');
            if (empty($id)){
                $this->error('用户ID不存在',U('admin_list'),1);
            }
            $status=M('admin')->where(array('id'=>$id))->getField('status');//判断当前状态情况
            if($status==1){
                $statedata = array('status'=>0);
                M('admin')->where(array('id'=>$id))->setField($statedata);
                $this->success('状态禁止',1,1);
            }else{
                $statedata = array('status'=>1);
                M('admin')->where(array('id'=>$id))->setField($statedata);
                $this->success('状态开启',1,1);
            }
    }

    public function admin_del(){
        $del_info=M('admin')->where(array('id'=>I('id')))->delete();
        if($del_info)
            $this->success('删除成功',U('Sys/admin_list'));
        else
            $this->error('删除失败');
    }

    public function admin_edit(){
        $admin=M('admin')->where(array('id'=>I('id')))->find();
        $this->assign('admin',$admin);
        $this->display();
    }

    public function admin_runedit(){
        $admin=M('admin');
        if (!$admin->autoCheckToken($_POST)){
            $this->error('表单令牌错误',0,0);
        }else{
            $data=I();
            if (I('password')!=''){
                $data['password']=I('password','','md5');
            }
            else{
                unset($data['password']);
            }
            if($admin->save($data)){
                $this->success('管理员修改成功',U('admin_list'),1);
            }
            else{
                $this->error('修改失败',0,0);
            }
        }
    }

    public function admin_add(){
        $this->display();
    }

    public function admin_runadd(){
        $admin=M('admin');
        if (!$admin->autoCheckToken($_POST)){
            $this->error('表单令牌错误',0,0);
        }else{
            $check_user=$admin->where(array('username'=>I('username')))->find();
            if ($check_user){
                $this->error('用户已存在，请重新输入用户名',0,0);
            }
            else{
                $data = I();
                $data['pwd']=I('e','','md5');
                $data['reg_ip']=get_client_ip();
                $data['create_time']=time();
                if($admin->add($data))
                    $this->success('管理员添加成功',U('admin_list'),1);
            }
        }
    }
}